package com.ebeijia.service.user.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.ebeijia.dto.ApplyInfoDto;
import com.ebeijia.exception.ServiceException;
import com.ebeijia.mybatis.mapper.ApplyDetailInfoMapper;
import com.ebeijia.mybatis.mapper.ApplyInfoMapper;
import com.ebeijia.mybatis.mapper.CommonUserMapper;
import com.ebeijia.mybatis.mapper.UserInfoMapper;
import com.ebeijia.mybatis.pojo.ApplyDetailInfo;
import com.ebeijia.mybatis.pojo.ApplyInfo;
import com.ebeijia.mybatis.pojo.UserInfo;
import com.ebeijia.service.user.OtherService;

public class OtherServiceImpl implements OtherService {

	@Autowired
	private ApplyInfoMapper applyInfoMapper;
	@Autowired
	private ApplyDetailInfoMapper applyDetailMapper;
	@Autowired
	private UserInfoMapper userInfoMapper;
	@Autowired
	private CommonUserMapper commonUserMapper;

	@Override
	public int genApplyInfoDeatil(ApplyInfoDto applyInfoDto)
			throws ServiceException {

		ApplyInfo applyInfo = new ApplyInfo();
		BeanUtils.copyProperties(applyInfoDto, applyInfo);
		applyInfo.setApplyDate(new Date());

		Map<String, Object> param = new HashMap<String, Object>();
		UserInfo userInfo = userInfoMapper.selectByParams(param);

		applyInfo.setApplyUserId(userInfo.getId());
		applyInfo.setApplyNo(commonUserMapper.genApplyNo());
		applyInfoMapper.insert(applyInfo);

		ApplyDetailInfo applyDetail = new ApplyDetailInfo();
		applyDetail.setApplyNo(applyInfo.getApplyNo());
		BeanUtils.copyProperties(applyInfoDto, applyDetail);
		applyDetailMapper.insert(applyDetail);
		
		return 1;
	}
}
